package com.kuang.demo01;

/**
 * @author luomingliang
 * @date 2021/5/12 21:26
 */
public class SaleTicketDemo01 {
    public static void main(String[] args) {
        // 并发：多线程操作同一个资源类，把资源丢入线程
        Ticket ticket = new Ticket();
        Runnable runnable = () -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        };
        new Thread(runnable, "A").start();
        new Thread(runnable, "B").start();
        new Thread(runnable, "C").start();
    }
}

//资源类 OOP
class Ticket {
    //属性 方法
    private int num = 30;

    //卖票的方式
    //synchronized 本质：队列，锁
    public synchronized void sale() {
        if (num > 0) {
            System.out.println(Thread.currentThread().getName()+"买了一张票，剩余"+(--num));
        }
    }
}
